#!/bin/bash

FREQS=(528000000 1068000000 1560000000 2112000000)
SLEEP_SEC=300
DMC_FREQ_NODE="/sys/class/devfreq/dmc/userspace/set_freq"
DMC_GOVERNOR_NODE="/sys/class/devfreq/dmc/governor"

# 节点存在性检查
if [ ! -e "$DMC_FREQ_NODE" ]; then
    echo "错误：未找到 DDR 频率设计节点：$DMC_FREQ_NODE"
    echo "尝试设置 governor 为userspace 模式..."

    if [ -w "$DMC_GOVERNOR_NODE" ]; then
        echo userspace > "$DMC_GOVERNOR_NODE"
        sleep 0.5
    else
        echo "无法写入 governor node ($DMC_GOVERNOR_NODE)，请检查权限或者路径"
        exit 1
    fi

# 再次检测
    if [ ! -e "$DMC_FREQ_NODE" ]; then
        echo "设置 governor 后仍未找到 DDR 频率设置节点：$DMC_FREQ_NODE"
        exit
    else
        echo "成功启用userspace模式，已找到频率设置节点"
    fi
else
    echo "已检测到频率设置节点：$DMC_FREQ_NODE"
fi

echo "开始 DDR 变频..."

# 循环频率设置
while true; do
    for freq in "${FREQS[@]}"; do
        echo "➡️ 切换到 DDR 频率: $((freq/1000000)) MHz @ $(date '+%F %T')"
        echo $freq > "$DMC_FREQ_NODE"
        sleep $SLEEP_SEC
    done
done




